package com.inspire.springboot

import com.inspire.springboot.kt.study.Singleton
import com.inspire.springboot.kt.study.Study
import com.inspire.springboot.kt.study.entity.Person
import com.inspire.springboot.kt.study.entity.Student

/**
 * @author     ：inspire
 * @date       ：Created in 2024/5/13 14:43
 * @description：
 * @modified By：inspire
 * @version:版本
 */
val b = 1//变量1
val c = 3
val range = 0..10

fun main() {
    println("Hello World!")

    println(b)//注释
    println(c)
    println(add(1, 5))
    checkNumber(0.1)
    forTest()

    var person = Person("bb", 12)
    person.name = "aa"
    person.age = 0
    person.printInfo()

    val student1 = Student("aa", 12, "1234", 1)
    student1.study()

    val student2 = Student("bb", 13, "1235")

    val student3 = Student()
    Singleton.a()
    listTest1()

    threadTest()

    study(student1)
    study(null)

    println(getScore("Tom"))
}

fun add(a: Int, b: Int): Int {
    if (a > b) {
        return a;
    } else {
        return b;
    }
}

fun add2(a: Int, b: Int): Int {
    return if (a > b) a else b
}

fun add3(a: Int, b: Int) = if (a > b) a else b

fun checkNumber(num: Number) {
    when (num) {
        is Int -> println("Int")
        is Double -> println("Double")
//        else -> println("others")
    }
}

fun getScore(name: String) = when {
    name.startsWith("Tom") -> 86
    name == "Jim" -> 77
    else -> 0
}

fun forTest() {
    for (i in 0 until 10 step 2) {
        println("a:"+i)
    }
}

fun listTest1(){
    val list = listOf<String>("a", "aba", "aabb", "a")
    var maxStr = ""
    for (str in list) {
        if (str.length > maxStr.length) {
            maxStr = str;
        }
    }
    println(maxStr)

    listTest2()
}

fun listTest2(){
    val list = listOf<String>("a", "aba", "aabb", "a")
    var lambda = {str: String -> str.length}
    var maxStr = list.maxByOrNull(lambda)
    println(maxStr)

    listTest3()
}

fun listTest3(){
    val list = listOf<String>("a", "aba", "aabb", "a")
    var maxStr = list.maxByOrNull({str: String -> str.length})
    println(maxStr)
}

fun threadTest(){
    Thread(object:Runnable{
        override fun run() {
            println("Thread test!")
        }
    }).start()

    Thread(Runnable { println("Thread test2!") }).start()

    Thread({ println("Thread test3!")}).start()

    Thread{ println("Thread test4!")}.start()
}

fun study(study: Study?){//?表示study参数可为空
    study?.doHomework()
    study?.readBooks()

    study2(study)
}

fun study2(study:Study?){
    study?.let {
        it.doHomework()
        it.readBooks()
    }
}